﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using mvcDemo.Models;
using System.Web;

namespace mvcDemo.Controllers
{
    public class PicsController : ApiController
    {
        // GET api/pic
        /// <summary>
        /// 获取所有相片
        /// </summary>
        /// <returns></returns>
        public JsonMessage Get()
        {
            try
            {
                using (Entities db = new Entities())
                {
                    List<PIC> pics = db.PIC.ToList();
                    return new JsonMessage(pics);
                }                
            }
            catch (Exception ex)
            {
                return new JsonMessage("获取相片失败！", ex.Message, ex.StackTrace);
            }
        }

        // GET api/pic/5       
        /// <summary>
        /// 获取指定相片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonMessage Get(int id)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    PIC pic = db.PIC.Find(id);
                    return new JsonMessage(pic);
                }
            }
            catch (Exception ex)
            {
                return new JsonMessage("获取相片失败！", ex.Message, ex.StackTrace);
            }
        }

        // POST api/pics
        /// <summary>
        /// 保存上传的图片到数据库,返回上传的记录
        /// </summary>
        /// <returns>[{rows}]</returns>
        public JsonMessage Post()
        {
            try
            {
                if (Request.Content.IsMimeMultipartContent())
                {
                    HttpFileCollection files = HttpContext.Current.Request.Files;
                    if (files.Count > 0)
                    {                       
                        List<object> outPic = new List<object>();
                        for (int i = 0; i < files.Count; i++)
                        {
                            using (Entities db = new Entities())
                            {
                                HttpPostedFile currFile = files[i];
                                int fileLength = currFile.ContentLength;
                                byte[] fileByte = new byte[fileLength];
                                currFile.InputStream.Read(fileByte, 0, fileLength);
                                string newFullName = Guid.NewGuid().ToString();
                                string origFullName = currFile.FileName;
                                string fileOrigName = currFile.FileName.LastIndexOf(".") > 0 ? origFullName.Substring(0, currFile.FileName.LastIndexOf(".")) : origFullName;
                                PIC pic = new PIC()
                                {
                                    FILENAME = fileOrigName,
                                    FILEFULLNAME = newFullName,
                                    ORIGNAME = origFullName,
                                };
                                db.PIC.Add(pic);
                                db.SaveChanges();
                                PIC newPic = db.PIC.Single(a => a.FILEFULLNAME == newFullName);
                                PICDATA picData = new PICDATA()
                                {
                                    ID = newPic.ID,
                                    FILEDATA = fileByte
                                };
                                db.PICDATA.Add(picData);
                                db.SaveChanges();
                                outPic.Add(newPic);
                            }
                            
                            //OracleParameter[] parameters = paramList.ToArray();                                               
                            //return sbCmdText.ToString();
                        }
                        return new JsonMessage(outPic);
                    }
                    else
                    {
                        return new JsonMessage("请选择正确的文件！", "");
                    }
                }
                else
                {
                    return new JsonMessage("请先选择文件！", "");
                }
            }
            catch (Exception ex)
            {
                return new JsonMessage("上传图片失败！", ex.Message, ex.StackTrace);
            }
        }

        // DELETE api/pics/5
        /// <summary>
        /// 删除指定相片
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonMessage Delete(int id)
        {
            try
            {
                using (Entities db = new Entities())
                {
                    PIC pic = db.PIC.Find(id);
                    db.PIC.Remove(pic);
                    db.SaveChanges();
                }
                return new JsonMessage();
            }
            catch (Exception ex)
            {
                return new JsonMessage("删除失败！", ex.Message, ex.StackTrace);
            }
        }
    }
}
